<?php

if (!defined('APP_RUN'))
	exit();

class View extends SimpleO {

	private $vars = array();
	private $view = null;
	private $smarty = false;

	public function __construct($_name, $_smarty = false) {
		parent::__construct();
		$this->view = $_name;
		$this->smarty = $_smarty;
	}

	public function __get($_key) {
		if (array_key_exists($_key, $this->vars))
			return $this->vars[$_key];
	}

	public function __set($_key, $_value) {
		$this->vars[$_key] = $_value;
	}

	public function render() {
		if (!$this->smarty) {
			extract($this->vars);
			include_once('system/view/' . $this->view . '.php');
		} else {
			$path = explode('/', $this->view);
			$file = $path[count($path) - 1];
			$path = array_slice($path, 0, count($path) - 1);
			$path = implode('/', $path);
			require_once('system/libsmarty/smarty.php');
			$smarty = new Smarty;
			$smarty->template_dir = 'system/view/' . $path;
			$smarty->config_dir = 'system/lib/smarty/configs';
			$smarty->cache_dir = 'system/lib/smarty/cache';
			$smarty->compile_dir = 'system/lib/smarty/compile';
			$smarty->assign('path', PATH);
			$smarty->assign('logged', LOGGED);
			$smarty->assign('config', $this->config);
			$smarty->assign('archez', $this->archez);
			$smarty->assign('ots', $this->ots);
			$smarty->assign('db', $this->db);
			foreach ($this->vars as $key => $value) {
				$smarty->assign($key, $value);
			}
			$smarty->display($file . '.tpl');
		}
	}

}

?>